package com.burgeon.backend.SHOPBACKEND.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.Order;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QueryOrder extends RESTBase {
    private final static String RANGE = "50";//分页数量
    private String ip;
    private String account;
    private String password;

    public QueryOrder(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 接收REP返回的字符串并进行数据处理
     *
     * @return
     */
    public ArrayList<Order> getData(int start, String status, String disStatus) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(start, status, disStatus), url);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Order> orderArrayList = new ArrayList<Order>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);

            Order order = new Order();
            order.setId(jsonArray.getJSONArray(i).getString(0));
            order.setDocno(jsonArray.getJSONArray(i).getString(1));
            order.setBilldate(jsonArray.getJSONArray(i).getString(2));
            order.setXsno(jsonArray.getJSONArray(i).getString(3));
            order.setYcsotype(jsonArray.getJSONArray(i).getString(4));
            order.setDescription(jsonArray.getJSONArray(i).getString(5));
            order.setReceiverName(jsonArray.getJSONArray(i).getString(6));
            order.setReceiverMobile(jsonArray.getJSONArray(i).getString(7));
            order.setReceiverAddress(jsonArray.getJSONArray(i).getString(8));
            order.setAddressDesc(jsonArray.getJSONArray(i).getString(9));
            order.setStatus(jsonArray.getJSONArray(i).getString(10));
            order.setDisStatus(jsonArray.getJSONArray(i).getString(11));
            order.setIsUp(jsonArray.getJSONArray(i).getString(12));
            order.setTotLines(jsonArray.getJSONArray(i).getString(13));
            order.setTotQty(jsonArray.getJSONArray(i).getString(14));
            order.setTotAmtList(jsonArray.getJSONArray(i).getString(15));
            order.setAddressDesc(jsonArray.getJSONArray(i).getString(16));

            orderArrayList.add(order);
        }

        return orderArrayList;
    }

    /**
     * 准备向ERP传送字段拼接
     *
     * @return
     */
    private MultiValueMap Data(int y, String status, String disStatus) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("ID");//ID
        columns.add("DOCNO");// 单据编号
        columns.add("BILLDATE");// 	单据日期
        columns.add("XSNO");//线上单号
        columns.add("YC_SOTYPE");//订单类型
        columns.add("DESCRIPTION");//备注
        columns.add("RECEIVER_NAME");// 收货人姓名
        columns.add("RECEIVER_MOBILE");//收货人手机
        columns.add("RECEIVER_ADDRESS");//收货人的详细地址
        columns.add("ADDRESS_DESC");//地址备注
        columns.add("STATUS");//提交状态
        columns.add("DIS_STATUS");// 分单状态
        columns.add("IS_UP");//同步状态
        columns.add("TOT_LINES");//总行数
        columns.add("TOT_QTY");//总数量
        columns.add("TOT_AMT_LIST");//总零售金额
        columns.add("TOT_AMT_ACTUAL");//总成交金额

        params.put("columns", columns);
        params.put("table", "YC_RETAIL");//表名

        //设置查询规则（从第几条查询，每页显示多少条数）
        params.put("range", RANGE);
        params.put("count", "true");
        int r = Integer.valueOf(RANGE);
        params.put("start", String.valueOf(y * r));

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        expr1.put("column", "STATUS");
        expr1.put("condition", status);

        expr2.put("column", "DIS_STATUS");
        expr2.put("condition", disStatus);

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);
        params.put("params", paramss);
        return DATA("Query", params, account, password);

    }
    /**
     * 执行方法，向ERP传参
     *
     * @param args
     */
    public static void main(String[] args) {
        QueryOrder queryOrder = new QueryOrder("39.100.45.8:55322", "nea@burgeon.com.cn", "123");

        try {
            System.err.println(queryOrder.getData(1, "2", "2"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}